package me.zixing.java.chapter11.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by bingh on 2017/12/1.
 */
public class StaticWebServer implements Server{

    private int port;
    private ServerSocket ServerSocket;

    public StaticWebServer() {
    }

    public StaticWebServer(int port) {
        this.port = port;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public void start() {
        if(this.port == 0){
            port = 8080;
        }
        try{
            ServerSocket serverSocket = new ServerSocket(port);
            while(true){
                Socket socket = serverSocket.accept();
                ServerHandler serverHandler = new ServerHandler(socket);
                serverHandler.start();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onStart() {

    }

    @Override
    public void close() {

    }

    @Override
    public void conClose() {

    }

    public static void main(String args[]){
        StaticWebServer server = new StaticWebServer(8080);
        server.start();
    }
}
